package us.icitap.cm.ui.policecase;

import java.util.Arrays;

import us.icitap.cm.CaseManagementUI;
import us.icitap.cm.business.AppListTypeEnum;
import us.icitap.cm.business.entities.AppEntity;
import us.icitap.cm.business.entities.AppList;
import us.icitap.cm.business.entities.EntityTypeEnum;
import us.icitap.cm.business.entities.Person;
import us.icitap.cm.business.entities.SexEnum;
import us.icitap.cm.business.entities.regmin.States;
import us.icitap.cm.listeners.AppEntityListener;
import us.icitap.cm.ui.custom.CustBeanFieldGroup;
import us.icitap.cm.ui.custom.CustComboBox;
import us.icitap.cm.ui.custom.CustDateField;
import us.icitap.cm.ui.custom.CustTextField;
import us.icitap.cm.ui.custom.CustomFormLayout;
import us.icitap.cm.ui.shared.AliasField;
import us.icitap.cm.ui.shared.AppEntityLayout;
import us.icitap.cm.utils.ProjectLists;
import us.icitap.utils.FormatingUtils;

import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.AbstractSelect.ItemCaptionMode;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CheckBox;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.VerticalLayout;

public class PersonDataLayout extends AppEntityLayout  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8154251086482434649L;

	private Person person;

	private CustTextField nameTextField = new CustTextField("Emri");
	private CustTextField surnameTextField = new CustTextField("Mbiemri");
	private CustTextField fatherTextField = new CustTextField("Babai");
	private CustTextField motherTextField = new CustTextField("Nena");
	private CustDateField birthDateDateField = new CustDateField("Datelindja");
	private CustTextField ageTextField = new CustTextField("Mosha e perafert");
	private CustComboBox sexSelect = new CustComboBox("Seksi");
	private CustComboBox nationalitySelect = new CustComboBox("Kombesia");
	private CustComboBox educationSelect = new CustComboBox("Edukimi");
	private CustComboBox professionSelect = new CustComboBox("Profesioni");
	
	private CustTextField celNoTextField = new CustTextField("Nr. cel");
	private CustTextField phoneNoTextField = new CustTextField("Nr. tel");
	

	private CustTextField heightTextField = new CustTextField("Gjatesia(cm)");
	private CustTextField weightTextField = new CustTextField("Pesha(kg)");
	
	private CheckBox alcoholInfluenceCheck = new CheckBox("Ndikim alkoli");
	private CheckBox alcoInflOverLimitCheck = new CheckBox("Ndikim alkoli mbi limitin e lejuar");
	private CheckBox drugInfluenceCheck = new CheckBox("Ndikim nga droga");
	private CustTextField otherInfluenceCheck = new CustTextField("Ndikime te tjera");
	private CustComboBox personStatus = new CustComboBox("Statusi i Personit");
	private CustComboBox personInvolvementSelect = new CustComboBox("Perfshirja ne ngjarje");
	private CustComboBox fileTypeSelect = new CustComboBox("Kertela e personit");
	
	
	
	private CustComboBox physicalPsychicStatusSelect = new CustComboBox("Gjendja fizike/psikike");
	private CustComboBox specialQualitySelect = new CustComboBox("Cilesi e vecante");
	private CustComboBox eyeColorSelect = new CustComboBox("Ngjyra e syve");
	private  CustComboBox hairColorSelect = new CustComboBox("Ngjyra e flokeve");
	private CustComboBox humanRaceSelect = new CustComboBox("Perkatesia etnike");
	
	private AppEntityListener listener;
	private final CustBeanFieldGroup<Person> binder = new CustBeanFieldGroup<Person>(Person.class);
	final CustomFormLayout cfl = new CustomFormLayout("600px", "Person i perfshire ne ngjarje", "Reset", "Ruaj");
	
	final TabSheet tabSheet = new TabSheet();
	
	private AliasField aliasField = new AliasField("Nofkat");
	


	boolean editableVisible = false;
	boolean readOnlyVisible = false;

	private void _init(){
		this.sexSelect.setWidth("4em");
		this.birthDateDateField.setDateFormat(FormatingUtils.SHORT_DATE_FORMAT);
		this.ageTextField.setWidth("4em");
		this.heightTextField.setWidth("4em");
		this.weightTextField.setWidth("4em");
	}
	
	
	public PersonDataLayout() {
		this.setMargin(true);
		
		this._init();
		
		HorizontalLayout hl = new HorizontalLayout();
		hl.setSizeFull();
		hl.addStyleName("cmLayout");
		hl.setMargin(true);
		FormLayout fl = new FormLayout();
		fl.addComponent(this.personInvolvementSelect);
		fl.addComponent(this.nameTextField);
		fl.addComponent(this.surnameTextField);
		fl.addComponent(this.fatherTextField);
		fl.addComponent(this.motherTextField);
		fl.addComponent(this.sexSelect);
		fl.addComponent(this.birthDateDateField);
		fl.addComponent(this.ageTextField);
		fl.addComponent(this.nationalitySelect);
		fl.addComponent(this.educationSelect);
		fl.addComponent(this.professionSelect);
		fl.addComponent(this.specialQualitySelect);
		fl.addComponent(this.celNoTextField);
		fl.addComponent(this.phoneNoTextField);
		
		hl.addComponent(fl);
		tabSheet.addTab(hl, "Info baze");
		
		hl = new HorizontalLayout();
		hl.addStyleName("cmLayout");
		hl.setSizeFull();
		
		fl = new FormLayout();
		fl.setMargin(true);
//		fl.addComponent(aliasComponent);
		fl.addComponent(aliasField);
		fl.addComponent(this.heightTextField);
		fl.addComponent(this.weightTextField);
		fl.addComponent(this.eyeColorSelect);
		fl.addComponent(this.hairColorSelect);
		fl.addComponent(this.humanRaceSelect);
		fl.addComponent(this.physicalPsychicStatusSelect);
		fl.addComponent(this.alcoholInfluenceCheck);
		fl.addComponent(this.alcoInflOverLimitCheck);
		fl.addComponent(this.drugInfluenceCheck);
		fl.addComponent(this.otherInfluenceCheck);
		fl.addComponent(this.personStatus);
		fl.addComponent(this.fileTypeSelect);
		hl.addComponent(fl);
		tabSheet.addTab(hl, "Info shtese");
		
		
		cfl.addComponent(tabSheet);
		cfl.addFooter();
		
		sexSelect.setContainerDataSource(new BeanItemContainer<SexEnum>(SexEnum.class, Arrays.asList(SexEnum.values())));

		
		nationalitySelect.setContainerDataSource(new BeanItemContainer<States>(States.class, ProjectLists.getStatesList(false)));
		nationalitySelect.setItemCaptionMode(ItemCaptionMode.EXPLICIT);
		nationalitySelect.setItemCaptionPropertyId("tag");
		
		educationSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.Education, false)));
		
		
		professionSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.Profession, false)));
		
		specialQualitySelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.SpecialQuality, false)));
		
		personStatus.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PersonStatus, false)));

		
		physicalPsychicStatusSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PhysicalPsychicStatus, false)));
		
		eyeColorSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.EyeColor, false)));
		
		hairColorSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.HairColor, false)));
		
		humanRaceSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.HumanRace, false)));
		
		personInvolvementSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PersonInvolvement, false)));
		
		
		
		fileTypeSelect.setContainerDataSource(new BeanItemContainer<AppList>(AppList.class, ProjectLists.getList(AppListTypeEnum.PersonFileType, false)));
		
		
		aliasField.setImmediate(true);
		
		binder.bind(personInvolvementSelect, "involvement");
		binder.bind(nameTextField, "name");
		binder.bind(surnameTextField, "surname");
		binder.bind(fatherTextField, "fatherName");
		binder.bind(motherTextField, "motherName");
		binder.bind(sexSelect, "sex");
		binder.bind(birthDateDateField, "birthDate");
		binder.bind(ageTextField, "age");
		binder.bind(nationalitySelect, "nationality");
		binder.bind(educationSelect, "education");
		binder.bind(professionSelect, "profession");
		binder.bind(specialQualitySelect, "specialQuality");
		binder.bind(celNoTextField, "celNo");
		binder.bind(phoneNoTextField, "phoneNo");
		
		
		binder.bind(heightTextField, "height");
		binder.bind(weightTextField, "weight");
		binder.bind(eyeColorSelect, "eyeColor");
		binder.bind(hairColorSelect, "hairColor");
		binder.bind(humanRaceSelect, "humanRace");
		binder.bind(physicalPsychicStatusSelect, "physicalPsychicStatus");
		binder.bind(alcoholInfluenceCheck, "alcoholInfluence");
		binder.bind(alcoInflOverLimitCheck, "alcoInflOverLimit");
		binder.bind(drugInfluenceCheck, "drugsInfluence");
		binder.bind(otherInfluenceCheck, "otherInfluence");
		binder.bind(personStatus, "personStatus");
		binder.bind(fileTypeSelect, "fileType");
		binder.bind(aliasField, "aliasList");
		
		cfl.addRightClickListener(new ClickListener() {
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 7920984069687317979L;

			@Override
			public void buttonClick(ClickEvent event) {
				try {
					binder.commit();
					
					if(PersonDataLayout.this.listener != null){
						if(oldAppEntity == null)
							PersonDataLayout.this.listener.addAppEntityToReport(person.getAppEntity());
						else{
							if(!oldAppEntity.getPerson().myEquals(person)){
								person.getAppEntity().setRecordUser(CaseManagementUI.getUser().getUsername());
								PersonDataLayout.this.listener.updateAppEntityIntoReport(oldAppEntity, person.getAppEntity());
							}
							else{
//								oldAppEntity.replaceEntityReportAssociationList(person.getAppEntity().getReportAssociationList());
								PersonDataLayout.this.listener.refresh(oldAppEntity);
							}
								
						}
							
						addAppEntity();
					}
						
					
				} catch (CommitException e) {
					e.printStackTrace();
				}
				
				
			}
		});
		
	}

	
	
	public void showEditableForm(){
		if(editableVisible) return ;
		
		this.removeAllComponents();
		this.addComponent(cfl);
		
		editableVisible = true;
		readOnlyVisible = false;
	}

	public void showReadOnlyForm(){
		if(readOnlyVisible) return;
		this.removeAllComponents();
		VerticalLayout vl = ViewCaseLayout.showPerson(person);
		vl.setCaption("Person i perfshire ne ngjarje");
		this.addComponent(vl);
		editableVisible = false;
		readOnlyVisible = true;
	}
	

	@Override
	public void addAppEntity() {

		this.tabSheet.setSelectedTab(0);
		AppEntity appEntity = new AppEntity(CaseManagementUI.getUser().getUsername(), EntityTypeEnum.PERSON);
		person = appEntity.getPerson();
		aliasField.setPerson(person);
		binder.setItemDataSource(person);
		this.showEditableForm();
			
	}


	@Override
	public void addAppEntityListener(AppEntityListener listener) {
		this.listener = listener;
		
	}

	private AppEntity oldAppEntity;
	

	@Override
	public void loadAppEntity(AppEntity appEntity, boolean editable) {
		try {
			oldAppEntity = appEntity;
			
			AppEntity newAppEntity = new AppEntity(appEntity);
			
			
			
			person = newAppEntity.getPerson();
			aliasField.setPerson(person);
			
			binder.setItemDataSource(person);
			if(editable) 
				this.showEditableForm();
			else 
				this.showReadOnlyForm();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}




}
